package com.woniuxy.factory.util;

import com.woniuxy.factory.vo.MenuVO;

import java.util.ArrayList;
import java.util.List;

/**
 * @NTLX data 2020 08 22
 */
public class TreeUtil {

    public static List<MenuVO> toTree(List<MenuVO> treeList, Integer pid) {
        //新建容器
        List<MenuVO> retList = new ArrayList<MenuVO>();
        //循环变量集合
        for (MenuVO parent : treeList) {
            //判断当前元素的子节点
            if (pid.equals(parent.getPid())) {
                //调用递归
                retList.add(findChildren(parent, treeList));
            }
        }
        return retList;
    }
    private static MenuVO findChildren(MenuVO parent, List<MenuVO> treeList) {
        //循环
        for (MenuVO child : treeList) {
            //元素子节点判断
            if (parent.getId().equals(child.getPid())) {
                if (parent.getChild() == null) {
                    parent.setChild(new ArrayList<>());
                }
                //递归
                parent.getChild().add(findChildren(child, treeList));
            }
        }
        return parent;
    }
}
